package com.shanghaionstar.user.service;

import com.onstar.cn.api.UsersService;
import com.onstar.cn.common.exception.OnStarException;
import com.onstar.cn.common.util.Constants;

import com.onstar.cn.framework.annotation.http.HeadsValue;
import com.onstar.cn.framework.annotation.http.Request;
import com.onstar.cn.framework.mvp.base.core.BaseService;
import com.onstar.cn.framework.network.HttpInjectUtils;
import com.onstar.cn.framework.network.entity.RequestEntity;
import com.onstar.cn.framework.network.enumtype.HttpAccept;
import com.onstar.cn.framework.network.enumtype.HttpMethod;
import com.onstar.cn.framework.network.enumtype.Language;
import com.onstar.cn.framework.utils.GsonHelper;
import com.onstar.cn.smart.BaseResponse;
import com.shanghaionstar.OnstarApplication;
import com.shanghaionstar.user.bean.ReplenishEntity;
import com.shanghaionstar.utils.ApplicationUtil;
import com.shanghaionstar.utils.BusinessUtil;
import com.shanghaionstar.utils.NetConnectUtil;

import rx.Observable;
import rx.functions.Func0;

/**
 * Created by Yonglang Hong on 2016/11/15.
 */
public class ReplenishModel extends BaseService{

    @Override
    public RequestEntity getRequestEntity() {
        return null;
    }

    @Override
    public Object jsonToBean(String json) {
        return null;
    }

    public Observable<BaseResponse> replenish(final ReplenishEntity entity) {
        return Observable.defer(new Func0<Observable<BaseResponse>>() {
            @Override
            public Observable<BaseResponse> call() {
                try {
                    return Observable.just(doReplenish(entity));
                } catch (OnStarException e) {
                    return Observable.error(e);
                }
            }
        });
    }

    @Request(heads = @HeadsValue(accept = HttpAccept.JSON, language = Language.AUTO), method = HttpMethod.PUT)
    public BaseResponse doReplenish(ReplenishEntity entity) throws OnStarException {
        UsersService service = new UsersService(Constants.CURRENT_SERVER_URL);
        RequestEntity requestEntity = NetConnectUtil.getAuthRequestEntity(OnstarApplication.getApplication());
        String body = GsonHelper.toJson(entity);
        service.doReplenish(requestEntity ,body);
//        requestEntity.setBody(body);
//        requestEntity.setUrl("http://113.52.176.197:51611/mspV3/api/v3/user/register/info3/update");
        requestEntity = HttpInjectUtils.injectHttp(getClass(), "doReplenish", requestEntity,ReplenishEntity.class);
        String responseStr = NetConnectUtil.getResponse(getDatas(requestEntity));
        BaseResponse passResponse = GsonHelper.fromJson(responseStr,BaseResponse.class);
        return passResponse;
    }


    public Observable<BaseResponse> validateNeedName(final String vin) {
        return Observable.defer(new Func0<Observable<BaseResponse>>() {
            @Override
            public Observable<BaseResponse> call() {
                try {
                    return Observable.just(doValidateNeedName(vin));
                } catch (OnStarException e) {
                    return Observable.error(e);
                }
            }
        });
    }

    @Request(heads = @HeadsValue(accept = HttpAccept.JSON, language = Language.AUTO), method = HttpMethod.GET)
    public BaseResponse doValidateNeedName(String vin) throws OnStarException {
        UsersService service = new UsersService(Constants.CURRENT_SERVER_URL);
        RequestEntity requestEntity = NetConnectUtil.getAuthRequestEntity(OnstarApplication.getApplication());
        service.validateNeedName(requestEntity , ApplicationUtil.getInstance().getIdpID(),vin);
        requestEntity = HttpInjectUtils.injectHttp(getClass(), "doValidateNeedName", requestEntity,String.class);
        String responseStr = NetConnectUtil.getResponse(getDatas(requestEntity));
        BaseResponse passResponse = GsonHelper.fromJson( responseStr,BaseResponse.class);
        return passResponse;
    }
}
